package com.cxf.windex.base.rest;

import com.cxf.windex.base.secruity.JwtUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 * @author chenxf
 * @date 4/11/2018
 * @description
 */
public class BaseController {
  public String getCurrentUserId(){
      SecurityContext sc = SecurityContextHolder.getContext();
      if(sc.getAuthentication()==null){
        return "";
      }
      Object principal = sc.getAuthentication().getDetails();
      if (principal instanceof JwtUser) {
        JwtUser user = (JwtUser) principal;
        return user.getId();
      }
   return "";
  }
  public String getCurrentName() {
    SecurityContext sc = SecurityContextHolder.getContext();
    if (sc.getAuthentication() == null) {
      return "";
    }
    Object principal = sc.getAuthentication().getDetails();
    if (principal instanceof JwtUser) {
      JwtUser user = (JwtUser) principal;
      return user.getName();
    }
    return "";
  }

  public String getCurrentUserType() {
    SecurityContext sc = SecurityContextHolder.getContext();
    if (sc.getAuthentication() == null) {
      return "0";
    }
    Object principal = sc.getAuthentication().getDetails();
    if (principal instanceof JwtUser) {
      JwtUser user = (JwtUser) principal;
      return user.getTypeId();
    }
    return "0";
  }


}
